package com.flink.wc.demo.demo_20240801;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

public class Demo_02 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        SingleOutputStreamOperator<Tuple2<String, Integer>> outputStreamOperator = env
                .socketTextStream("192.168.105.34", 666)
                .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
                        String[] split = s.split(",");
                        for (String s1 : split) {
                            collector.collect(new Tuple2<String, Integer>(s1, 1));
                        }
                    }
                })
                .keyBy(line -> line.f0)
                .window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(2)))
                .sum(1);
        outputStreamOperator.print();
        env.execute();
    }
}
